from selenium import webdriver
from selenium.webdriver.common.by import By
import pytest
import time


# 登录相关的用例
def loginAndCheck(username, password):
    wd = webdriver.Chrome()
    wd.implicitly_wait(10)
    wd.get("http://127.0.0.1/mgr/sign.html")
    if username is not None:
        wd.find_element(By.ID, "username").send_keys(username)
    if password is not None:
        wd.find_element(By.ID, "password").send_keys(password)
    wd.find_element(By.CSS_SELECTOR, "button[type='submit']").click()
    time.sleep(1)
    alertText = wd.switch_to.alert.text
    print(alertText)
    wd.quit()
    return alertText


# 用于前置条件的登录功能
@pytest.fixture(scope="module", autouse=True)
def adminOp():
    wd = webdriver.Chrome()
    wd.implicitly_wait(10)
    wd.get("http://127.0.0.1/mgr/sign.html")
    wd.find_element(By.ID, "username").send_keys('byhy')
    wd.find_element(By.ID, "password").send_keys('88888888')
    wd.find_element(By.CSS_SELECTOR, "button[type='submit']").click()
    time.sleep(1)
    # 登录成功，进行一个存档，后续的用例可直接往后执行
    yield wd
    wd.quit()

